import contextMenuEvent from '../resources/contextMenu';
import {
	addResCollect,
	delResCollectByResId
} from '@/api/data/tbNetCollect';
import {
	renameFolder,
	renameResource,
	downloadResNoLog,
	banDeleteResource,
	updateSecretSetting
} from '@/api/data/tbNetDisk';
export default {
	components: {},
	props: {
		btnList: {
			type: Array,
			default: () => []
		}
	},
	data() {
		return {

		}
	},
	methods: {
		//右键菜单
		handleContextMenu(row, column, event) {
			this.$contextmenu({
				event,
				minWidth: 80,
				customClass: 'resource_context_menu',
				items: contextMenuEvent(row, this.btnList, this.handleEvent)
			})
		},

		//操作
		handleEvent(data, type) {
			switch (type) {
				case 'publish': //发布
					this.handlePublish(data);
					break;
				case 'editlink': //编辑链接
					this.handleEditLink(data);
					break;
				case 'download': //下载
					this.handleDownload(data);
					break;
				case 'catalog': //编目
					this.handleEditCatalog(data);
					break;
				case 'label': //标签
					this.handleEditLabel(data);
					break;
				case 'secret': //密级
					this.handleAmendSecret(data);
					break;
				case 'share': //分享
					this.handleShare(data);
					break;
				case 'collect': //收藏
					this.handleCollect(data);
					break;
				case 'inform': //举报
					this.handleInform(data);
					break;
				case 'rename': //重命名
					this.handleRename(data);
					break;
				case 'move': //移动到
					this.handleMoveTo(data);
					break;
				case 'copy': //复制到
					this.handleCopyTo(data);
					break;
				case 'delete': //删除
					this.handleDelete(data);
					break;
			}
		},

		//
		handleMsgBoxEvent(opt) {
			this.$msgbox({
				title: opt.title,
				message: opt.message,
				showCancelButton: true,
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				async beforeClose(action, instance, done) {
					if (action === 'confirm') {
						instance.confirmButtonLoading = true;
						await opt.confirm();
						instance.confirmButtonLoading = false;
						done();
					} else {
						done();
					}
				}
			}).catch(() => {});
		},

		//
		handlePromptEvent(title, message, opt) {
			this.$prompt(message, title, {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				inputType: opt.type || 'text',
				inputValue: opt.value,
				closeOnClickModal: false,
				inputPlaceholder: opt.placeholder,
				inputErrorMessage: opt.errorMessage,
				dangerouslyUseHTMLString: true,
				inputValidator(value) {
					return !!value
				},
				async beforeClose(action, instance, done) {
					if (action === 'confirm') {
						instance.confirmButtonLoading = true;
						await opt.confirm(instance.inputValue);
						instance.confirmButtonLoading = false;
						done();
					} else {
						done();
					}
				}
			}).catch(() => {});
		},

		//
		handleConfirmEvent(title, message, callback) {
			this.$confirm(message, title, {
				type: 'warning',
				dangerouslyUseHTMLString: true,
				async beforeClose(action, instance, done) {
					if (action === 'confirm') {
						instance.confirmButtonLoading = true;
						await callback();
						instance.confirmButtonLoading = false;
						done();
					} else {
						done();
					}
				}
			}).catch(() => {});
		},

		//发布
		handlePublish(data) {
			this.$emit('hand', 'publish', data);
		},

		//编辑链接
		handleEditLink(data) {
			this.$emit('hand', 'editlink', data);
		},

		//下载
		async handleDownload(data) {
			if (data.isLocal === '2') {
				if (data.showType === '1') {
					this.$getImageSuffix(data.attrAddr).then(res => {
						let name = `${data.oldName}.${res}`;
						this.$fileDownload(data.attrAddr, name);
					})
				}
			} else {
				let name = `${data.oldName.split('.')[0]}`
			}
		},

		//编辑编目
		handleEditCatalog(data) {
			this.$emit('hand', 'catalog', data);
		},

		//编辑标签
		handleEditLabel(data) {
			this.$emit('hand', 'label', data);
		},

		//修改密级
		handleAmendSecret(data) {
			let that = this;
			let createEle = that.$createElement;
			that.resourceSecret = data.resLevel;
			that.handleMsgBoxEvent({
				title: '修改密级',
				message: createEle('div', null, [
					createEle('p', {
						style: {
							'margin-bottom': '10px'
						}
					}, '请选择资源等级'),
					createEle('sw-select', {
						attrs: {
							size: 'medium'
						},
						props: {
							data: that.dictEnums['RESLEVEL'],
							prop: {
								label: 'msg',
								value: 'value'
							},
							value: that.resourceSecret,
							placeholder: '资源等级',
							'popper-append-to-body': false
						},
						style: {
							width: '100%'
						},
						on: {
							input: (e) => {
								that.resourceSecret = e;
							}
						}
					})
				]),
				async confirm() {
					let params = {
						id: data.id,
						oldName: data.oldName,
						resLevel: that.resourceSecret
					};
					let result = await updateSecretSetting(params).then(res => res.data);
					if (result.code == '0') {
						that.$message.success('修改密级成功');
						data.resLevel = that.resourceSecret;
					} else {
						that.$message.success('修改密级失败');
					}
				}
			})
		},

		//分享
		handleShare(data) {
			this.$emit('hand', 'share', data)
		},

		//收藏/取消收藏操作
		async handleCollect(data) {
			let isCollect = data.isCollect === '1';
			let params = {
				pcId: data.id,
				pcName: data.oldName
			};
			let callFunction = eval(isCollect ? delResCollectByResId : addResCollect);
			let result = await callFunction(isCollect ? data.id : params).then(res => res.data);
			if (result.code == '0') {
				data.isCollect = isCollect ? '2' : '1';
				this.$message.success(isCollect ? '取消收藏成功' : '资源收藏成功')
			} else {
				this.$message.error(isCollect ? '取消收藏失败' : '资源收藏失败')
			}
		},

		//举报
		handleInform(data) {
			let that = this;
			that.handlePromptEvent('举报', '您举报资源的原因', {
				type: 'textarea',
				placeholder: '举报原因',
				errorMessage: '请输入举报原因',
				async confirm(content) {
					let params = {
						pcId: data.id,
						pcName: data.oldName,
						type: '1',
						content
					}
					let result = await addedResInform(params).then(res => res.data);
					if (result.code == '0') {
						that.$message.success('资源举报成功');
					} else {
						that.$message.error('资源举报失败');
					}
				}
			})
		},

		//重命名
		handleRename(data) {
			let that = this;
			that.handlePromptEvent('重命名', '输入新的文件名称', {
				value: data.oldName,
				placeholder: '举报原因',
				errorMessage: '请输入文件名称',
				async confirm(value) {
					let isFolder = data.isFolder === '1';
					let params = {
						id: data.id,
						oldName: value
					}
					let callFunction = eval(isFolder ? renameFolder : renameResource);
					let result = await callFunction(params).then(res => res.data);
					if (result.code === '0') {
						that.$message.success(`重命名${isFolder ? '文件夹' : '资源'}成功`);
						data.oldName = value;
					} else {
						that.$message.error(`重命名${isFolder ? '文件夹' : '资源'}失败`);
					}
				}
			})
		},

		//移动到
		async handleMoveTo(data) {
			this.$emit('hand', 'move', data);
			/* let that = this;
			let createEle = that.$createElement;
			let folderId = data.pid === '0' ? data.dirId : data.pid;
			that.handleMsgBoxEvent({
				title: '移动到',
				message: createEle('libraryTree', {
					style: { width: '100%' },
					attrs: { size: 'medium' },
					class: 'library_tree_wrapper',
					props: { value: that.resourceSecret, folderId },
					on: {
						input: (e) => {
							that.resourceSecret = e;
						}
					}
				}),
				async confirm() {
					console.log(that.resourceSecret);
				}
			})
			console.log(data); */
			/* let params = { page: 1, limit: 9999, isFolder: '1', dirId: data.id };
			let result = await getLibraryList(params).then(res => res.data);
			if (result.code === '0') {
				let createEle = that.$createElement;
				that.handleMsgBoxEvent({
					title: '移动到',
					message: createEle('libraryTree', null, [
						createEle('p', { style: { 'margin-bottom': '10px' } }, '请选择资源等级'),
						createEle('elTree', {
							attrs: { size: 'medium' },
							props: {
								data: that.dictEnums['RESLEVEL'],
								prop: { label: 'msg', value: 'value' },
								value: that.resourceSecret,
								placeholder: '资源等级',
								'popper-append-to-body': false
							},
							style: { width: '100%' },
							on: {
								input: (e) => {
									that.resourceSecret = e;
								}
							}
						})
					]),
					async confirm() {
						let params = { id: data.id, oldName: data.oldName, resLevel: that.resourceSecret };
						let result = await updateSecretSetting(params).then(res => res.data);
						if (result.code === '0') {
							that.$message.success('修改密级成功');
							data.resLevel = that.resourceSecret;
						} else {
							that.$message.success('修改密级失败');
						}
					}
				})
			}
			console.log(result); */
		},

		//复制到
		handleCopyTo(data) {
			this.$emit('hand', 'copy', data);
		},

		//删除
		handleDelete(data) {
			let that = this;
			let isFolder = data.isFolder === '1';
			let tipMessage = `此操作将删除${isFolder ? '文件夹' : '资源'}<font color="#f60">【${data.oldName}】</font>, 是否继续?`;
			that.handleConfirmEvent('删除提示', tipMessage, async () => {
				let result = await banDeleteResource(data.id).then(res => res.data);
				if (result.code === '0') {
					let index = that.data.findIndex(item => item.id === data.id)
					that.$message.success('删除资源成功');
					that.data.splice(index, 1);
				} else {
					that.$message.error('删除资源失败');
				}
			})
		}
	}
}